package com.mallcloud.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.product.api.entity.ShopCartItemModel;
import com.mallcloud.mall.product.service.ShopCartItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/shopCartItem")
public class ShopCartItemController {
    private final ShopCartItemService shopCartItemService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_view')")
    public R<IPage<ShopCartItemModel>> page(Page page, ShopCartItemModel dto) {
        return R.ok(shopCartItemService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_view')")
    public R<List<ShopCartItemModel>> list(ShopCartItemModel dto) {
        return R.ok(shopCartItemService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_view')")
    public R<ShopCartItemModel> getInfo(ShopCartItemModel dto) {
        return R.ok(shopCartItemService.getById(dto.getCartItemId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_add')")
    public R<Boolean> save(@RequestBody ShopCartItemModel dto) {
        return R.ok(shopCartItemService.save(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_edit')")
    public R<Boolean> updateById(@RequestBody ShopCartItemModel dto) {
        return R.ok(shopCartItemService.updateById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{cartItemId}")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_del')")
    public R<Boolean> removeById(@PathVariable Integer cartItemId) {
        return R.ok(shopCartItemService.removeById(cartItemId));
    }

    @ApiOperation("批量删除购物车")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> cartItemIds) {
        return R.ok(shopCartItemService.removeByIds(cartItemIds));
    }

    @ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('product_shopcartitem_export')")
    public List<ShopCartItemModel> export(ShopCartItemModel shocartItem) {
        return shopCartItemService.list(Wrappers.query(shocartItem));
    }
}
